﻿<UserControl x:Class="Samba.Presentation.Terminal.SelectedTicketItemEditorView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:FxButton="clr-namespace:FlexButton;assembly=FlexButton" xmlns:VirtualKeyboard="clr-namespace:Samba.Presentation.Common.VirtualKeyboard;assembly=Samba.Presentation.Common" mc:Ignorable="d" 
             d:DesignHeight="320" d:DesignWidth="240">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid Margin="-1" Background="{StaticResource {x:Static SystemColors.InfoBrushKey}}"  Visibility="{Binding IsEditorsVisible,Converter={StaticResource VisibilityConverter}}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock FontWeight="Bold" FontSize="12" Padding="1" Grid.Column="0" Text="{Binding SelectedItem.Quantity,StringFormat='#.##'}" Margin="5,1,6,1"/>
            <TextBlock FontWeight="Bold" FontSize="12" Padding="1" Grid.Column="1" Text="{Binding SelectedItem.Description}" TextTrimming="CharacterEllipsis" Margin="1"/>
            <TextBlock FontWeight="Bold" FontSize="12" Padding="1" Grid.Column="2" Text="{Binding SelectedItem.TotalPrice, StringFormat='#,#0.00'}" Margin="1" HorizontalAlignment="Right"/>
        </Grid>

        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="auto"/>
            </Grid.RowDefinitions>

            <StackPanel Grid.Row="0" Margin="1" Visibility="{Binding IsEditorsVisible,Converter={StaticResource VisibilityConverter}}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="70*"/>
                        <ColumnDefinition Width="30*"/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Grid.Column="0" >
                        <TextBlock FontWeight="Bold" MouseDown="TextBlock_MouseDown" Text="{LocText ExtraModifier,Suffix=':'}"></TextBlock>
                        <TextBox Margin="2" Name="ExtraPropertyName" Text="{Binding SelectedItem.CustomPropertyName}" GotKeyboardFocus="ExtraPropertyName_GotKeyboardFocus" LostKeyboardFocus="ExtraPropertyName_LostKeyboardFocus"></TextBox>
                    </StackPanel>
                    <StackPanel Grid.Column="1">
                        <TextBlock FontWeight="Bold" MouseDown="TextBlock_MouseDown_1" Text="{LocText Price,Suffix=':'}"></TextBlock>
                        <TextBox Margin="2" Name="ExtraPropertyPrice" Text="{Binding SelectedItem.CustomPropertyPrice}" GotKeyboardFocus="ExtraPropertyPrice_GotKeyboardFocus" LostFocus="ExtraPropertyPrice_LostFocus"></TextBox>
                    </StackPanel>
                </Grid>
            </StackPanel>

            <StackPanel Grid.Row="1" Visibility="{Binding IsPortionsVisible,Converter={StaticResource VisibilityConverter}}">
                <TextBlock FontWeight="Bold" Text="{LocText Portions,Suffix=':'}"/>
                <ItemsControl ItemsSource="{Binding SelectedItemPortions}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Columns="4"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <FxButton:FlexButton 
                                Margin="0,1,1,0" MinHeight="30"
                                ButtonColor="{Binding Name, Converter={StaticResource PortionColorConverter},UpdateSourceTrigger=PropertyChanged}" 
                                Command="{Binding DataContext.PortionSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ItemsControl}}}" 
                                CommandParameter="{Binding}">
                                <TextBlock Margin="2" TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Name}"/>
                            </FxButton:FlexButton>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>

            <TextBox Name="NoteEditor" Grid.Row="2" Text="{Binding TicketNote}" Visibility="{Binding IsTicketNoteVisible,Converter={StaticResource VisibilityConverter}}" IsVisibleChanged="TextBox_IsVisibleChanged" TextWrapping="Wrap" AcceptsReturn="True" />

            <Grid Grid.Row="2" Visibility="{Binding IsTagEditorVisible,Converter={StaticResource VisibilityConverter}}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <TextBox Name="FreeTagEditor" Text="{Binding CustomTag,UpdateSourceTrigger=PropertyChanged}"  Visibility="{Binding IsFreeTagEditorVisible,Converter={StaticResource VisibilityConverter}}" GotFocus="FreeTagEditor_GotFocus"/>
                <FxButton:FlexButton Grid.Column="1" Command="{Binding AddTicketTagCommand}" Content="{Binding AddTicketTagCommand.Caption}" Visibility="{Binding IsFreeTagEditorVisible,Converter={StaticResource VisibilityConverter}}"/>
                <ItemsControl Grid.Row="2" Grid.ColumnSpan="2" ItemsSource="{Binding TicketTags}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid VerticalAlignment="Top" Columns="{Binding TagColumnCount}"></UniformGrid>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <FxButton:FlexButton 
                                            Margin="0,1,1,0" MinHeight="30" 
                                            ButtonColor="{Binding Name, Converter={StaticResource TagColorConverter}}" 
                                            Command="{Binding DataContext.TicketTagSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}" 
                                            CommandParameter="{Binding}">
                                <TextBlock TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Display}"/>
                            </FxButton:FlexButton>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>

            <ItemsControl Grid.Row="3" 
                          ItemsSource="{Binding SelectedItemGroupedPropertyItems}" 
                          Visibility="{Binding IsEditorsVisible,Converter={StaticResource VisibilityConverter}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <DockPanel Margin="1">
                            <TextBlock DockPanel.Dock="Top" FontWeight="Bold" Text="{Binding Name}"/>
                            <ItemsControl DockPanel.Dock="Bottom" ItemsSource="{Binding Properties}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <UniformGrid Columns="{Binding TerminalColumnCount}"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <FxButton:FlexButton 
                                            Height="{Binding DataContext.TerminalButtonHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}"
                                            Margin="0,1,1,0"  
                                            ButtonColor="{Binding TicketItemProperty, Converter={StaticResource PropertyColorConverter}}" 
                                            Command="{Binding DataContext.PropertyGroupSelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=2, AncestorType={x:Type ItemsControl}}}" 
                                            CommandParameter="{Binding}">
                                            <TextBlock TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Name}"/>
                                        </FxButton:FlexButton>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DockPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            
            <ItemsControl Grid.Row="3" 
                          ItemsSource="{Binding SelectedItemPropertyGroups}" 
                          Visibility="{Binding IsEditorsVisible,Converter={StaticResource VisibilityConverter}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <DockPanel Margin="1">
                            <TextBlock DockPanel.Dock="Top" FontWeight="Bold" Text="{Binding Name}"/>
                            <ItemsControl DockPanel.Dock="Bottom" ItemsSource="{Binding Properties}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <UniformGrid Columns="{Binding TerminalColumnCount}"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <FxButton:FlexButton 
                                            Height="{Binding DataContext.TerminalButtonHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}"
                                            Margin="0,1,1,0"  
                                            ButtonColor="{Binding TicketItemProperty, Converter={StaticResource PropertyColorConverter}}" 
                                            Command="{Binding DataContext.PropertySelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=2, AncestorType={x:Type ItemsControl}}}" 
                                            CommandParameter="{Binding}">
                                            <TextBlock TextWrapping="Wrap" TextAlignment="Center" Text="{Binding Display}"/>
                                        </FxButton:FlexButton>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DockPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

            <Viewbox Grid.Row="4" VerticalAlignment="Bottom" Visibility="{Binding IsKeyboardVisible, Mode=TwoWay,Converter={StaticResource VisibilityConverter}}">
                <VirtualKeyboard:KeyboardView2 ></VirtualKeyboard:KeyboardView2>
            </Viewbox>

        </Grid>
    </Grid>
</UserControl>
